package com.yuanduan.mplus.view;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.cj.framework.app.BaseActivity;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.presenter.DriveScoreManager;
import com.yuanduan.mplus.presenter.FrameManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 驾驶评分界面
 */
public class DriverScore1Activity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
    //head
    private TextView tv_title;
    private Spinner spinner;
    private ImageView iv_back;
    private TextView tv_share;

    //first part
    private BarChart barChart;

    //second part
    private RadarChart radarChart;

    private ProgressDialog progressDialog;
    private DriveScoreManager manager;

    private FrameManager frameManager;

    @Override
    protected void findId() {
        //head
        tv_share= (TextView) findViewById(R.id.drivescore1_share);
        tv_title= (TextView) findViewById(R.id.drivescore1_tv_title);
        iv_back= (ImageView) findViewById(R.id.drivescore1_iv_back);
        spinner= (Spinner) findViewById(R.id.drivescore1_sp);

        //firstpart
        barChart= (BarChart) findViewById(R.id.drivescore1_barchart);

        //second part
        radarChart= (RadarChart) findViewById(R.id.drivescore1_radarchart);
    }

    @Override
    protected void logic() {
        manager=new DriveScoreManager(this);
        manager.run("18857541457",0);
        frameManager=new FrameManager(this);
        progressDialog=new ProgressDialog(this);
        progressDialog.setMessage("正在加载，请稍候");
        progressDialog.show();
        initBarchart();
        iv_back.setOnClickListener(this);
        String[] items={"("+xValues.get(3)+")评分","("+xValues.get(2)+")评分","("+xValues.get(1)+")评分","("+xValues.get(0)+")评分"};
        spinner.setAdapter(new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,items));
        spinner.setOnItemSelectedListener(this);
        tv_share.setOnClickListener(this);

        radarChart.getLegend().setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        radarChart.setDescription("");
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_driver_score1;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.destroy();
        frameManager.destroy();
    }

    private ArrayList<String> legends=new ArrayList<>();
    private ArrayList<String> xValues=new ArrayList<>();
    private void initBarchart(){
        legends.add("安全");
        legends.add("经济");
        manager.setXValues(xValues);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.drivescore1_iv_back:
                this.finish();
                break;
            case R.id.drivescore1_share:
                frameManager.show();
                break;
        }
    }

    public void setBarChartData(List<ArrayList<Float>> barchartData){
        ArrayList<IBarDataSet> sets=new ArrayList<>();
        for(int i=0;i<barchartData.size();i++){
            ArrayList<BarEntry> entries=new ArrayList<>();
            for(int j=0;j<barchartData.get(i).size();j++){
                BarEntry entry=new BarEntry(barchartData.get(i).get(j),j);
                entries.add(entry);
            }
            IBarDataSet set=new BarDataSet(entries,legends.get(i));
            sets.add(set);
        }
        ((BarDataSet)sets.get(0)).setColor(getResources().getColor(R.color.blue1));
        ((BarDataSet)sets.get(1)).setColor(Color.RED);
        BarData barData=new BarData(xValues,sets);
        barChart.setDescription("");
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getLegend().setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        barChart.getXAxis().setDrawGridLines(false);
        barChart.getAxisRight().setDrawLabels(false);
        barChart.getAxisRight().setDrawAxisLine(false);
        barChart.getAxisLeft().setDrawAxisLine(false);
        barChart.setData(barData);
        barChart.animateY(3000, Easing.EasingOption.EaseInCubic);
    }

    public void setRadarChartData(List<ArrayList<Float>> radarChartData){
        ArrayList<IRadarDataSet> sets=new ArrayList<>();
        for(int i=0;i<radarChartData.size();i++){
            ArrayList<Entry> entries=new ArrayList<>();
            for(int j=0;j<radarChartData.get(i).size();j++){
                Entry entry=new Entry(radarChartData.get(i).get(j),j);
                entries.add(entry);
            }
            IRadarDataSet set=new RadarDataSet(entries,"safety");
            sets.add(set);
        }
        ((RadarDataSet)sets.get(0)).setColor(getResources().getColor(R.color.blue1));
        ((RadarDataSet)sets.get(1)).setColor(Color.RED);
        RadarData radarData=new RadarData(new String[]{"偏道","车距","碰撞","超速","平稳度"},sets);
        radarChart.setData(radarData);
        radarChart.animateY(3000, Easing.EasingOption.EaseInCubic);

    }

    public void invalidateBarChart(){
        barChart.animateY(3000, Easing.EasingOption.EaseInCubic);
    }

    public void invalidateRadarBarChart(){
        radarChart.animateY(3000, Easing.EasingOption.EaseInCubic);
    }

    public void setError(){
        manager.run("18857541457",0);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if(position==0){
            invalidateBarChart();
            invalidateRadarBarChart();
        }else{
            manager.run("",position);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public void dismissDialog(){
        progressDialog.dismiss();
    }

}
